home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 14 / develop 14 code / Writing Localizable Apps / Globalize.c next >
Encoding:
Text File  |  1993-03-09  |  4.8 KB  |  157 lines  |  [TEXT/KAHL]

  1. /**********************************************************************************
  2.  
  3.     NOTE: This file won't compile as-is, though each sample has been compiled on its
  4.     own. The code here has been extracted from the article simply for your convenience, 
  5.     so you won't have to type it in. Take what you need, and adapt it for your own 
  6.     devious purposes.
  7.     
  8. ***********************************************************************************/
  9.  
  10.  
  11. //--------------------------------------
  12. //    An event loop that's two-byte savvy
  13.  
  14. // Globals
  15. unsigned short    gCharBuf;        // Buffer that holds our (possibly two-byte) 
  16.                                 // character
  17. Boolean            gNeed2ndByte;    // Flag that tells us we're waiting for the
  18.                                 // second byte of a two-byte character
  19.  
  20. void EventLoop(void)
  21. {
  22.     EventRecord        event;            // The current event
  23.     short            cbResult;        // The results of our CharByte call
  24.     unsigned char    oneByte;        // Single byte extracted from event
  25.     Boolean            processChar;    // Whether we should send our application 
  26.                                     // a key message
  27.     
  28.     if (WaitNextEvent(everyEvent, &event, SleepTime(), nil)) {
  29.         switch (event.what) {
  30.             //. . .
  31.             case keyDown:
  32.             case autoKey:
  33.                 //. . .
  34.                 // Your code checks for Command-key equivalents here.
  35.                 //. . .
  36.                 processChar = false;
  37.                 oneByte = (event.message & charCodeMask);
  38.                 if (gNeed2ndByte) {
  39.                     // We're expecting the second byte of a two-byte character. 
  40.                     // So OR the byte into the low byte of our accumulated
  41.                     // two-byte character.
  42.                     gCharBuf = (gCharBuf << 8) | oneByte;
  43.                     cbResult = CharByte((Ptr)&gCharBuf, 1);
  44.                     if (cbResult == smLastByte)
  45.                         processChar = true;
  46.                     gNeed2ndByte = false;
  47.                 } else {
  48.                     // We're not expecting anything in particular. We
  49.                     // might get a one-byte character, or we might get the
  50.                     // first byte of a two-byte character.
  51.                     gCharBuf = oneByte;
  52.                     cbResult = CharByte((Ptr)&gCharBuf, 1);
  53.                     if (cbResult == smFirstByte)
  54.                         gNeed2ndByte = true;
  55.                     else if (cbResult == smSingleByte)
  56.                         processChar = true;
  57.                 }
  58.                 
  59.                 // Now possibly send the typed character to the rest of the
  60.                 // application.
  61.                 if (processChar)
  62.                     AppKey(gCharBuf);
  63.                 break;
  64.  
  65.                 // case . . .
  66.         }
  67.     }
  68. }
  69.  
  70. //--------------------------------------
  71. //    Getting the current date and time
  72. //    into Pascal strings
  73.  
  74. #define kWantSeconds    true        // For IUTimeString
  75. #define kNoSeconds        false    
  76.  
  77. unsigned long        secs;
  78. Str255                theDate, theTime;
  79.  
  80. // Get the current date and time into Pascal strings.
  81. GetDateTime(&secs);
  82. IUDateString(secs, shortDate, theDate);
  83. IUTimeString(secs, kNoSeconds, theTime);
  84.  
  85.  
  86. //--------------------------------------
  87. //    Formatting a number with FormatX2Str
  88.  
  89. #define kFormatStrs        128     // resource id of a STR# resource with 
  90.                                 // that contains format strings
  91.  
  92. OSErr FormatANum(short theFormat, extended theNum, Str255 theString)
  93. {
  94.     NItl4Handle            itl4;
  95.     OSErr                    err;
  96.     NumberParts            numberParts;
  97.     Str255                textFormatStr;    // "Textual" number format spec
  98.     NumFormatString    formatStr;        // Opaque number format
  99.  
  100.     // Load the 'itl4' and copy the NumberParts record out of it.
  101.     itl4 = (NItl4Handle)IUGetIntl(4);
  102.     if (itl4 == nil)
  103.         return resNotFound;
  104.     numberParts = *(NumberParts *)((char *)*itl4 +
  105.         (*itl4)->defPartsOffset);
  106.     // Get the format string, convert it to a NumFormatString, and then 
  107.     // use it to format the input number.
  108.     GetIndString(textFormatStr, kFormatStrs, theFormat);
  109.     err = Str2Format(textFormatStr, &numberParts, &formatStr);
  110.     if (err != noErr)
  111.         return err;
  112.     err = FormatX2Str(theNum, &formatStr, &numberParts, theString);
  113.     return err;
  114. }
  115.  
  116. //--------------------------------------
  117. //    Formatting a currency value.
  118. //    Uses the routine above.
  119.  
  120. OSErr FormatCurrency(extended theNum, Str255 theString)
  121. {
  122.     Intl0Hndl    itl0;
  123.     OSErr        err;
  124.     Str255        currencySymbol, formattedValue;
  125.  
  126.     // First, format the number like this: ##,###.00. FormatX2Str will
  127.     // replace the "," and "." separators appropriately for the font 
  128.     // script.
  129.     err = FormatANum(kCurrencyFormat, theNum, formattedValue);
  130.     if (err != noErr)
  131.         return err;
  132.     
  133.     // Get the currency symbol from the 'itl0' resource. The currency 
  134.     // symbol is stored as up to three bytes. If any of the bytes aren't
  135.     // used they're set to zero. So, we use strncpy to copy out the
  136.     // currency symbol as a C string and forcibly terminate it in case it's
  137.     // three bytes long.
  138.     itl0 = (Intl0Hndl)IUGetIntl(0);
  139.     if (itl0 == nil)
  140.         return resNotFound;
  141.     strncpy(currencySymbol, &(*itl0)->currSym1, 3);
  142.     currencySymbol[3] = 0x00;
  143.     c2pstr(currencySymbol);
  144.  
  145.     // Now put the currency symbol and the formatted value together
  146.     // according to the currency symbol position.
  147.     if ((*itl0)->currFmt & currSymLead) {
  148.         StringCopy(theString, currencySymbol);
  149.         StringAppend(theString, formattedValue);
  150.     } else {
  151.         StringCopy(theString, formattedValue);
  152.         StringAppend(theString, currencySymbol);
  153.     }
  154.     return noErr;
  155. }
  156.  
  157.